{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定一个非负整数 num，反复将各个位上的数字相加，直到结果为一位数。\n",
    "\n",
    "示例:\n",
    "\n",
    "输入: 38\n",
    "输出: 2 \n",
    "解释: 各位相加的过程为：3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数，所以返回 2。\n",
    "进阶:\n",
    "你可以不使用循环或者递归，且在 O(1) 时间复杂度内解决这个问题吗？\n",
    "\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/add-digits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 暴力破解 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    \"\"\"\n",
    "    :type num : int\n",
    "    :rtype :int\n",
    "    \"\"\"\n",
    "    def addDigists(self, num):\n",
    "        while num > 9:\n",
    "            sum = 0\n",
    "            while (num != 0):\n",
    "                sum += num % 10\n",
    "                num = int(num / 10)\n",
    "            num = sum\n",
    "        return num        \n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    Sol = Solution()\n",
    "    end = Sol.addDigists(383)\n",
    "    print(end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
